package com.bwda.dsrs.base.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author yangjun
 * @date 2019/3/18
 */
public class DateUtil {

    private static Pattern compilePattern = Pattern.compile("([-:\\s]?\\d+)");
    /**
     * 一周的天数为7天
     */
    private static final int DAYS_ONE_WEEK = 7;
    public static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN = "HH:mm";
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String DATE_TIME_PATTERN = DATE_PATTERN + " HH:mm:ss";
    public static final String FORMAT_ONE = "yyyy/MM/dd HH:mm:ss";


    /**
     * @param date
     * @param format
     * @return
     * @see
     */
    public static String formatDate(String date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(new SimpleDateFormat("yyyy-MM-dd").parse(date));
        } catch (ParseException e) {
            return "";
        }
    }

    /**
     * @param date
     * @param format
     * @return
     */
    public static String formatDate(Date date, String format) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }


    /**
     * 格式化时间
     *
     * @param calendar
     * @param format
     * @return
     */
    public static String formatDate(Calendar calendar, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(calendar.getTime());
    }

    /**
     * 增加时间对应的天数
     *
     * @param day
     * @return
     */
    public static Date addDateDay(int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }

    /**
     * 增加时间对应的天数
     *
     * @param date
     * @param day
     * @return
     */
    public static Date addDateDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }

    /**
     * 获取指定当前日期的后几天
     *
     * @param day
     * @return
     */
    public static Date getAssignDate(int day) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, day);
        return c.getTime();
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int daysBetween(Date date1, Date date2) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        long betweenDays = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(betweenDays));
    }

    public static void main(String[] args) {
        int df = daysBetween(new Date(), new Date());
        System.out.println("日期相差：" + df);
    }



    /**
     * 计算两个日期之间相差的毫秒数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long msBetween(Date date1, Date date2) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        long between = (time2 - time1);

        return between;
    }

    public static boolean isValidDate(String str) {
        boolean convertSuccess=false;
        List<SimpleDateFormat> simpleDateFormats = new ArrayList<>();
        simpleDateFormats.add(new SimpleDateFormat("yyyy/MM/dd"));
        simpleDateFormats.add(new SimpleDateFormat("yyyy-MM-dd"));
        simpleDateFormats.add(new SimpleDateFormat("yyyyMMdd"));
        for (SimpleDateFormat format:simpleDateFormats
        ) {
            try {
                format.setLenient(false);
                format.parse(str);
                convertSuccess=true;
                break;
            } catch (ParseException e) {
                convertSuccess=false;
            }

        }
        return convertSuccess;
    }

}
